#include "http/my_http_args.h"
#include "comm/my_log.h"

void test1()
{
    my_pool_t *pool = my_pool_create();
    my_http_args_t args;
    bzero(&args, sizeof(my_http_args_t));
    my_http_args_init(&args, pool);

    my_str_t str;
    MY_LOG_DEBUG("-----------------");
    MY_STR_SET(&str, "");
    my_http_args_parser(&args, &str);
    my_http_args_dump(&args);
    my_http_args_to_string(&args, &str);
    MY_LOG_DEBUG("%v", &str);

    my_pool_destroy(pool);
}

void test2()
{
    my_pool_t *pool = my_pool_create();
    my_http_args_t args;
    bzero(&args, sizeof(my_http_args_t));
    my_http_args_init(&args, pool);

    my_str_t str;
    MY_LOG_DEBUG("-----------------");
    MY_STR_SET(&str, "a");
    my_http_args_parser(&args, &str);
    my_http_args_dump(&args);
    my_http_args_to_string(&args, &str);
    MY_LOG_DEBUG("%v", &str);

    my_pool_destroy(pool);
}

void test3()
{
    my_pool_t *pool = my_pool_create();
    my_http_args_t args;
    bzero(&args, sizeof(my_http_args_t));
    my_http_args_init(&args, pool);

    my_str_t str;
    MY_LOG_DEBUG("-----------------");
    MY_STR_SET(&str, "a=");
    my_http_args_parser(&args, &str);
    my_http_args_dump(&args);
    my_http_args_to_string(&args, &str);
    MY_LOG_DEBUG("%v", &str);

    my_pool_destroy(pool);
}

void test4()
{
    my_pool_t *pool = my_pool_create();
    my_http_args_t args;
    bzero(&args, sizeof(my_http_args_t));
    my_http_args_init(&args, pool);

    my_str_t str;
    MY_LOG_DEBUG("-----------------");
    MY_STR_SET(&str, "=1");
    my_http_args_parser(&args, &str);
    my_http_args_dump(&args);
    my_http_args_to_string(&args, &str);
    MY_LOG_DEBUG("%v", &str);

    my_pool_destroy(pool);
}

void test5()
{
    my_pool_t *pool = my_pool_create();
    my_http_args_t args;
    bzero(&args, sizeof(my_http_args_t));
    my_http_args_init(&args, pool);

    my_str_t str;
    MY_LOG_DEBUG("-----------------");
    MY_STR_SET(&str, "a=&b=1");
    my_http_args_parser(&args, &str);
    my_http_args_dump(&args);
    my_http_args_to_string(&args, &str);
    MY_LOG_DEBUG("%v", &str);

    my_pool_destroy(pool);
}

void test6()
{
    my_pool_t *pool = my_pool_create();
    my_http_args_t args;
    bzero(&args, sizeof(my_http_args_t));
    my_http_args_init(&args, pool);

    my_str_t str;
    MY_LOG_DEBUG("-----------------");
    MY_STR_SET(&str, "a&b=1");
    my_http_args_parser(&args, &str);
    my_http_args_dump(&args);
    my_http_args_to_string(&args, &str);
    MY_LOG_DEBUG("%v", &str);

    my_pool_destroy(pool);
}

void test7()
{
    my_pool_t *pool = my_pool_create();
    my_http_args_t args;
    bzero(&args, sizeof(my_http_args_t));
    my_http_args_init(&args, pool);

    my_str_t str;
    MY_LOG_DEBUG("-----------------");
    MY_STR_SET(&str, "=1&b=1");
    my_http_args_parser(&args, &str);
    my_http_args_dump(&args);
    my_http_args_to_string(&args, &str);
    MY_LOG_DEBUG("%v", &str);

    my_pool_destroy(pool);
}

void test8()
{
    my_pool_t *pool = my_pool_create();
    my_http_args_t args;
    bzero(&args, sizeof(my_http_args_t));
    my_http_args_init(&args, pool);

    my_str_t str;
    MY_LOG_DEBUG("-----------------");
    MY_STR_SET(&str, "&b=1");
    my_http_args_parser(&args, &str);
    my_http_args_dump(&args);
    my_http_args_to_string(&args, &str);
    MY_LOG_DEBUG("%v", &str);

    my_pool_destroy(pool);
}

void test9()
{
    my_pool_t *pool = my_pool_create();
    my_http_args_t args;
    bzero(&args, sizeof(my_http_args_t));
    my_http_args_init(&args, pool);

    my_str_t str;
    MY_LOG_DEBUG("-----------------");
    MY_STR_SET(&str, "&&b=1");
    my_http_args_parser(&args, &str);
    my_http_args_dump(&args);
    my_http_args_to_string(&args, &str);
    MY_LOG_DEBUG("%v", &str);

    my_pool_destroy(pool);
}

void test10()
{
    my_pool_t *pool = my_pool_create();
    my_http_args_t args;
    bzero(&args, sizeof(my_http_args_t));
    my_http_args_init(&args, pool);

    my_str_t str;
    MY_LOG_DEBUG("-----------------");
    MY_STR_SET(&str, "a=1&b=2&c=3&=4&e&g=&f&&g=1&h&&i");
    MY_LOG_DEBUG("%v", &str);

    my_http_args_parser(&args, &str);
    my_http_args_dump(&args);
    my_http_args_to_string(&args, &str);
    MY_LOG_DEBUG("%v", &str);

    my_pool_destroy(pool);
}

void test11()
{
    my_pool_t *pool = my_pool_create();
    my_http_args_t args;
    bzero(&args, sizeof(my_http_args_t));
    my_http_args_init(&args, pool);

    my_str_t str;
    MY_LOG_DEBUG("-----------------");
    MY_STR_SET(&str, "a=1&a=2&c=3&a=4&e&g=&f&&g=1&h&&i&b=1&b=2&c=1");
    my_http_args_parser(&args, &str);

    my_str_t name;
    MY_STR_SET(&name, "c");
    my_http_arg_t *arg = my_http_args_get(&args, &name);
    MY_LOG_DEBUG("###");
    my_http_arg_dump(arg);

    MY_STR_SET(&name, "b");
    MY_LOG_DEBUG("###");
    my_http_same_args_t *same_args = my_http_args_values(&args, &name);
    my_http_same_args_dump(same_args);

    my_http_arg_t new_arg;
    MY_STR_SET(&new_arg.name, "b");
    MY_STR_SET(&new_arg.value, "123");
    my_http_args_add(&args, &new_arg);

    MY_LOG_DEBUG("###");
    same_args = my_http_args_values(&args, &name);
    my_http_same_args_dump(same_args);

    my_http_args_del(&args, &new_arg);

    MY_LOG_DEBUG("###");
    same_args = my_http_args_values(&args, &name);
    my_http_same_args_dump(same_args);

    my_http_args_del_by_name(&args, &name);

    MY_LOG_DEBUG("###");
    my_http_args_dump(&args);


    my_pool_destroy(pool);
}

int main()
{
    test1();
    test2();
    test3();
    test4();
    test5();
    test6();
    test7();
    test8();
    test9();
    test10();
    test11();

    my_mem_assert_leak();

    MY_LOG_DEBUG("test pass");
    return 0;
}